package gemini.castor.ui.client.page.content.home.apply.distributorid;

import gemini.basic.model.Distributor;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.page.content.home.apply.ApplyEvent;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class DistributorIDFormPresenter extends
		CastorWidgetPresenter<DistributorIDFormPresenter.Display> {

	@Inject
	public DistributorIDFormPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {
		HasClickHandlers getDistributorSubmitClickHandlers();
		HasClickHandlers getCancelClickHandlers();
		void setObject(DistributorIDFormObject inputObject);
		boolean isValidate();
		DistributorIDFormObject getObject();
		
	}
	
	private Object outputObject;
	private Distributor tempoDis;

	public static final Place PLACE = new Place(PlaceConstants.PLACE_REGISTER);

	@Override
	protected void onBind() {

		registerHandler(display.getDistributorSubmitClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						if(display.isValidate()){
							checkSponsorInfo();
						}
						
					}
				}));
		
		registerHandler(display.getCancelClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new ApplyEvent(
								ApplyEvent.ForwardType.CANCEL));
					}
				}));

		checkPersonalInfo();
	}
	
	private void checkPersonalInfo(){
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getDistributorService().save(
				new AbstractAsyncCallback<Distributor>() {

					@Override
					public void onSuccess(Distributor result) {
						eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
						tempoDis = result;
						initDisplay();
					}
				});
	}

	protected void checkSponsorInfo() {
		final String sponsorCode = display.getObject().getSponsor().getDistributorId();
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getDistributorService().getByCode(sponsorCode,
				new AbstractAsyncCallback<Distributor>() {

					@Override
					public void onSuccess(Distributor result) {
						eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
						if (result != null) {
							forwardIfSuccess(result);
						} else {
							GuiUtils.alert("Cannot find distributor with Code: <"
									+ sponsorCode
									+ "><br />Please check your sponsor info again!");
						}
					}
				});
	}


	private void initDisplay() {
		inputObject.setCode(tempoDis.getCode());
		display.setObject(inputObject);

		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
		display.loadData();
	}


	@Override
	public Place getPlace() {
		return PLACE;
	}
	
	private DistributorIDFormObject inputObject;
	
	@Override
	public void setInputObject(Object object){
		inputObject = (DistributorIDFormObject) object;
	}
	
	@Override
	public Object getOutputObject(){
		return outputObject;
	}
	
	protected void forwardIfSuccess(Distributor sponsor) {
		
		tempoDis.setDistributor(sponsor);
		tempoDis.setPin(display.getObject().getPin());
		tempoDis.setPersons(inputObject.getPersons());
		
		outputObject = tempoDis;
		localEventBus.fireEvent(new ApplyEvent(
				ApplyEvent.ForwardType.DISTRIBUTOR_SUBMIT));
		
	}
}
